package com.lw.netty.simple;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/4/24 11:41
 */
public class NettyClient {
    public static void main(String[] args) {
        //创建客户端
        Bootstrap bootstrap=new Bootstrap();
        //创建客户端线程组
        NioEventLoopGroup group=new NioEventLoopGroup();
        //配置客户端
        bootstrap.group(group).channel(NioSocketChannel.class)//设置客户端的通道
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        //初始化通道，并设置如何处理服务器端返回的数据
                        socketChannel.pipeline().addLast(new ClientHandler());
                    }
                });
        ChannelFuture channelFuture = bootstrap.connect("localhost", 12581);//连接指定的服务器
        try {
            channelFuture.channel().closeFuture().sync();//等待关闭客户端
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            group.shutdownGracefully();//关闭线程组
        }
    }
}
